home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Compendium Deluxe 2
/
LSD and 17bit Compendium Deluxe - Volume II.iso
/
a
/
prog
/
asmsrc
/
thesource-7.lha
/
Source
/
NumericalMethods.lha
/
NumericalMethods
/
atan2.c
next >
Wrap
Text File
|
1994-06-11
|
3KB
|
114 lines
/*
Path: usenet.ee.pdx.edu!cs.uoregon.edu!sgiblab!sgigate.sgi.com!olivea!decwrl!hookup!swrinde!ihnp4.ucsd.edu!usc!howland.reston.ans.net!pipex!uknet!EU.net!sun4nl!news.nic.surfnet.nl!tuegate.tue.nl!blade.stack.urc.tue.nl!jeanpaul
From: jeanpaul@blade.stack.urc.tue.nl (Jean-Paul Smeets)
Newsgroups: comp.graphics.algorithms
Subject: Re: Fast atan2(y/x) ?
Date: 11 Apr 1994 19:14:45 GMT
Organization: MCGV Stack, Eindhoven University of Technology, the Netherlands.
Lines: 108
Message-ID: <2oc7j5$chn@tuegate.tue.nl>
References: <2obpf5$8n8@hermes.louisville.edu>
NNTP-Posting-Host: blade.stack.urc.tue.nl
In article <2obpf5$8n8@hermes.louisville.edu>,
Joe Muller <jamull01@romulus.spd.louisville.edu> wrote:
>
> With all this talk about a fast square root algorithm I was wondering
>if anyone has a reasonably fast atan2 ? I have seen some approximations
>that return values from 0 to 255 but since I am already using floating point
>I need something that returns values from -PI to PI, or 0 to 360.
> I also need accuracy within +- 2 degrees, not -+ 5 or 10, so I was
>thinking along the lines of a power series with maybe a dozen or so iterations
I use a 10 bit fixed point atan2 in my real time code. It uses one division
and a table for one quadrant. The precision can be adapted by changing the
size of the lookup table.
Jean-Paul
*/
#define I_PI 3217
#define I_PI2 1608
static const int atan_table[] =
{
0, 4, 8, 12, 16, 20, 24, 28,
32, 36, 40, 44, 48, 52, 56, 60,
64, 68, 72, 76, 80, 84, 88, 92,
96, 100, 104, 108, 112, 116, 119, 123,
127, 131, 135, 139, 143, 147, 151, 155,
159, 163, 167, 170, 174, 178, 182, 186,
190, 194, 198, 201, 205, 209, 213, 217,
221, 224, 228, 232, 236, 240, 243, 247,
251, 255, 258, 262, 266, 270, 273, 277,
281, 284, 288, 292, 296, 299, 303, 307,
310, 314, 317, 321, 325, 328, 332, 335,
339, 343, 346, 350, 353, 357, 360, 364,
367, 371, 374, 378, 381, 385, 388, 392,
395, 399, 402, 405, 409, 412, 416, 419,
422, 426, 429, 432, 436, 439, 442, 446,
449, 452, 455, 459, 462, 465, 468, 472,
475, 478, 481, 484, 487, 491, 494, 497,
500, 503, 506, 509, 512, 516, 519, 522,
525, 528, 531, 534, 537, 540, 543, 546,
549, 552, 555, 557, 560, 563, 566, 569,
572, 575, 578, 581, 583, 586, 589, 592,
595, 597, 600, 603, 606, 609, 611, 614,
617, 619, 622, 625, 628, 630, 633, 636,
638, 641, 643, 646, 649, 651, 654, 656,
659, 662, 664, 667, 669, 672, 674, 677,
679, 682, 684, 687, 689, 691, 694, 696,
699, 701, 703, 706, 708, 711, 713, 715,
718, 720, 722, 725, 727, 729, 732, 734,
736, 738, 741, 743, 745, 747, 750, 752,
754, 756, 758, 760, 763, 765, 767, 769,
771, 773, 775, 778, 780, 782, 784, 786,
788, 790, 792, 794, 796, 798, 800, 802,
804
};
/* iatan2(y, x) = 1024 * atan2(y,x) */
int iatan2(int y, int x)
{
int xabs, yabs;
int f, g;
if (x >= 0)
xabs = x;
else
xabs = -x;
if (y >= 0)
yabs = y;
else
yabs = -y;
if (yabs <= xabs)
{
f = (yabs * 256) / xabs;
g = atan_table[f];
}
else
{
f = (xabs * 256) / yabs;
g = I_PI_2 - atan_table[f];
}
if (x >= 0)
{
if (y >= 0) /* first quadrant */
return(g);
else /* fourth quadrant */
return(-g);
}
else
{
if (y >= 0) /* second quadrant */
return(I_PI - g);
else /* third quadrant */
return(g - I_PI);
}
}